package com.jsbb.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * @author w
 * @date 2021-05-17
 */
public class DateV2Utils {

    public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";

    public static final String[] WEEK_NAME = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**
     * Date转字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 字符串转Date
     *
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date parse(String dateStr, String pattern) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern(pattern));
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    /**
     * 当前年月日时分秒
     *
     * @return
     */
    public static String getCurrentDateTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        return localDateTime.format(DateTimeFormatter.ofPattern(YMD_HMS));
    }

    /**
     * 当前年月日
     *
     * @return
     */
    public static String getCurrentDate() {
        return LocalDate.now().toString();
    }

    /**
     * 当前时分秒
     *
     * @return
     */
    public static String getCurrentTime() {
        return LocalTime.now().toString();
    }

    /**
     * 当前年
     *
     * @return
     */
    public static int getYear() {
        return LocalDate.now().getYear();
    }

    /**
     * 当前月
     *
     * @return
     */
    public static int getMonth() {
        return LocalDate.now().getMonthValue();
    }

    /**
     * 当前日
     *
     * @return
     */
    public static int getDay() {
        return LocalDate.now().getDayOfMonth();
    }

    /**
     * 当前星期几
     *
     * @return
     */
    public static String getDayOfWeek() {
        int dayOfWeek = LocalDate.now().getDayOfWeek().getValue();
        return WEEK_NAME[dayOfWeek];
    }

    /**
     * 当前时间戳
     *
     * @return
     */
    public static long getTimeStamp() {
        return Instant.now().toEpochMilli();
    }

    /**
     * 指定日期加X天
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        Instant instant = date.toInstant();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        localDateTime.plusDays(days);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static void main(String[] args) {
        System.out.println("Date转字符串 - " + format(new Date(), YMD_HMS));
        System.out.println("字符串转Date - " + parse(getCurrentDateTime(), YMD_HMS));
        System.out.println("当前年月日时分秒 - " + getCurrentDateTime());
        System.out.println("当前年月日 - " + getCurrentDate());
        System.out.println("当前时分秒 - " + getCurrentTime());
        System.out.println("当前年 - " + getYear());
        System.out.println("当前月 - " + getMonth());
        System.out.println("当前日 - " + getDay());
        System.out.println("当前星期几 - " + getDayOfWeek());
        System.out.println("当前时间戳 - " + getTimeStamp());

        System.out.println(addDay(new Date(), 1));
        System.out.println(format(addDay(new Date(), 2), YMD_HMS));
    }
}
